package vn.edu.saigontech.ncms.web.bookstore;

import java.util.Collection;
import java.util.Date;
import java.util.Set;

import vn.edu.saigontech.ncms.model.bookstore.Item;
import vn.edu.saigontech.ncms.model.bookstore.Return;
import vn.edu.saigontech.ncms.model.bookstore.ReturnDetail;
import vn.edu.saigontech.ncms.model.program.Level;
import vn.edu.saigontech.ncms.model.program.Program;
import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.ItemService;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.service.ReturnDetailService;
import vn.edu.saigontech.ncms.service.ReturnService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class ReturnAction extends BaseAction {
	private Long id;
	private String date;
	private Long userId;
	private String comment;
	
	// DETAILS
	private int quantity;
	private Long itemId;
	private Long detailId;
	
	// REDIRECT
	private Long programId;
	private Long levelId;
	
	private Return zeturn;
	private Set<ReturnDetail> details;
	
	private User searchUser;
	private Collection<Return> returns;
	private Collection<User> users;
	private Collection<Program> programs;

	public String showCreate() {
		this.programs = ProgramService.INSTANCE.findAllAreActive();
		this.users = UserService.INSTANCE.findByRole("Consulting");
		return SUCCESS;
	}
	
	public String create() {
		Return zeturn = new Return();
		Date date = DateUtil.fromString(this.date, this.i18n("format.date"));
		User returner = UserService.INSTANCE.findById(this.userId);
		zeturn.setReturner(returner);
		zeturn.setDate(date);
		zeturn.setComment(this.comment);
		
		zeturn = ReturnService.INSTANCE.persist(zeturn);
		
		// to re-direct to show add return details
		this.id = zeturn.getId();
		return SUCCESS;
	}
	
	public String showEdit() {
		this.zeturn = ReturnService.INSTANCE.findById(this.id);
		Level level = null;
		REQUEST().setAttribute("level", level);
		this.programs = ProgramService.INSTANCE.findAllAreActive();
		this.users = UserService.INSTANCE.findByRole("Consulting");
		return SUCCESS;
	}
	
	public String edit() {
		this.zeturn = ReturnService.INSTANCE.findById(this.id);
		Date date = DateUtil.fromString(this.date, this.i18n("format.date"));
		User returner = UserService.INSTANCE.findById(this.userId);
		zeturn.setReturner(returner);
		zeturn.setDate(date);
		zeturn.setComment(this.comment);
		
		zeturn = ReturnService.INSTANCE.persist(zeturn);
		return SUCCESS;
	}
	
	public String delete() {
		Return zeturn = ReturnService.INSTANCE.findById(this.id);
		Set<ReturnDetail> details = zeturn.getDetails();
		for(ReturnDetail detail : details) {
			Item item = detail.getItem();
			item.increaseQuantity(detail.getQuantity());
		}
		ReturnService.INSTANCE.deleteById(this.id);
		return SUCCESS;
	}
	
	public String showSearch() {
		this.users = UserService.INSTANCE.findByRole("Consulting");
		this.programs = ProgramService.INSTANCE.findAllAreActive();
		return SUCCESS;
	}
	
	public String search() {
		Date searchDate = Checker.isDate(this.date, this.i18n("format.date"))? DateUtil.fromString(this.date, this.i18n("format.date")) : null;
		this.searchUser = UserService.INSTANCE.findById(this.userId);
		this.returns = ReturnService.INSTANCE.find(searchDate, this.searchUser);
		this.users = UserService.INSTANCE.findByRole("Consulting");
		this.programs = ProgramService.INSTANCE.findAllAreActive();
		
		return SUCCESS;
	}
	
	public String showEditDetails() {
		this.zeturn = ReturnService.INSTANCE.findById(this.id);
		if(this.zeturn != null) {
			this.details = this.zeturn.getDetails();
		}
		this.programs = ProgramService.INSTANCE.findAll();
		return SUCCESS;
	}
	
	public String addDetail() {
		this.zeturn = ReturnService.INSTANCE.findById(this.id);
		
		ReturnDetail detail = new ReturnDetail();
		Item item = ItemService.INSTANCE.findById(this.itemId);
		detail.setItem(item);
		
		detail.setQuantity(this.quantity);
		item.decreseQuantity(this.quantity);
		
		detail.setZeturn(zeturn);
		
		detail = ReturnDetailService.INSTANCE.persist(detail);
		return SUCCESS;
	}
	
	public String deleteDetail() {
		ReturnDetail detail = ReturnDetailService.INSTANCE.findById(this.detailId);
		this.zeturn = detail.getZeturn();
		Item item = detail.getItem();
		item.increaseQuantity(detail.getQuantity());
		
		detail.setZeturn(null);
		
		ReturnDetailService.INSTANCE.deleteById(this.detailId);
		
		// to re-direct to show add return details
		this.id = zeturn.getId();
		return SUCCESS;
	}
	
	/*public String print() {
		this.zeturn = ReturnService.INSTANCE.findById(this.id);
		return SUCCESS;
	}*/
	
	public Collection<User> getUsers() {
		return users;
	}

	public void setUsers(Collection<User> users) {
		this.users = users;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public Long getDetailId() {
		return detailId;
	}

	public void setDetailId(Long detailId) {
		this.detailId = detailId;
	}

	public Collection<Program> getPrograms() {
		return programs;
	}

	public void setPrograms(Collection<Program> programs) {
		this.programs = programs;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Return getZeturn() {
		return zeturn;
	}

	public void setZeturn(Return zeturn) {
		this.zeturn = zeturn;
	}

	public Set<ReturnDetail> getDetails() {
		return details;
	}

	public void setDetails(Set<ReturnDetail> details) {
		this.details = details;
	}

	public Collection<Return> getReturns() {
		return returns;
	}

	public void setReturns(Collection<Return> returns) {
		this.returns = returns;
	}

	public User getSearchUser() {
		return searchUser;
	}

	public void setSearchUser(User searchUser) {
		this.searchUser = searchUser;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public Long getLevelId() {
		return levelId;
	}

	public void setLevelId(Long levelId) {
		this.levelId = levelId;
	}
}
